
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[TournamentGroupTournaments_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[TournamentGroupTournaments_Insert]

IF OBJECT_ID(N'[dbo].[TournamentGroupTournaments_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[TournamentGroupTournaments_Update]

IF OBJECT_ID(N'[dbo].[TournamentGroupTournaments_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[TournamentGroupTournaments_Delete]

IF OBJECT_ID(N'[dbo].[TournamentGroupTournaments_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[TournamentGroupTournaments_Select]

--endregion

GO


--region [dbo].[TournamentGroupTournaments_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[TournamentGroupTournaments_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@Id int = null,
	@IdGroupTournament int = null,
	@IdTournament int = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[TournamentGroupTournaments]
					WHERE
					(
						(@Id is null OR [TournamentGroupTournaments].[Id] = @Id)
						AND (@IdGroupTournament is null OR [TournamentGroupTournaments].[IdGroupTournament] = @IdGroupTournament)
						AND (@IdTournament is null OR [TournamentGroupTournaments].[IdTournament] = @IdTournament)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[TournamentGroupTournaments].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'Id' and @SortType = 1 THEN [TournamentGroupTournaments].[Id] END ASC,
						CASE WHEN @SortBy = 'Id' and @SortType = 0 THEN [TournamentGroupTournaments].[Id] END DESC,
						CASE WHEN @SortBy = 'IdGroupTournament' and @SortType = 1 THEN [TournamentGroupTournaments].[IdGroupTournament] END ASC,
						CASE WHEN @SortBy = 'IdGroupTournament' and @SortType = 0 THEN [TournamentGroupTournaments].[IdGroupTournament] END DESC,
						CASE WHEN @SortBy = 'IdTournament' and @SortType = 1 THEN [TournamentGroupTournaments].[IdTournament] END ASC,
						CASE WHEN @SortBy = 'IdTournament' and @SortType = 0 THEN [TournamentGroupTournaments].[IdTournament] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[TournamentGroupTournaments]
			Where 
			(
				(@Id is null OR [TournamentGroupTournaments].[Id] = @Id)
				AND (@IdGroupTournament is null OR [TournamentGroupTournaments].[IdGroupTournament] = @IdGroupTournament)
				AND (@IdTournament is null OR [TournamentGroupTournaments].[IdTournament] = @IdTournament)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[TournamentGroupTournaments_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[TournamentGroupTournaments_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[TournamentGroupTournaments_Insert]
	@Id int OUTPUT,
	@IdGroupTournament int,
	@IdTournament int

AS


INSERT INTO [dbo].[TournamentGroupTournaments] 
(
	[IdGroupTournament],
	[IdTournament]
)
VALUES 
(
	@IdGroupTournament,
	@IdTournament
)

SET @Id = SCOPE_IDENTITY()

--end [dbo].[TournamentGroupTournaments_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[TournamentGroupTournaments_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[TournamentGroupTournaments_Update]
	@Id int,
	@IdGroupTournament int,
	@IdTournament int
AS


UPDATE [dbo].[TournamentGroupTournaments] SET
	[IdGroupTournament] = @IdGroupTournament,
	[IdTournament] = @IdTournament
WHERE
	[Id] = @Id

--end [dbo].[TournamentGroupTournaments_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[TournamentGroupTournaments_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[TournamentGroupTournaments_Delete]
	@Id int
AS


DELETE FROM [dbo].[TournamentGroupTournaments]
WHERE
(
	[Id] = @Id
)

--end [dbo].[TournamentGroupTournaments_Delete]
--endregion

GO
--=========================================================================================--

